<?php

switch (@$URL[1]) 
{
    case 'upload_image':
        
        if (!empty($_FILES)) 
        {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_base_path .'/'. 'temp_pictures' . '/location/';
            $targetFile =  str_replace('//','/',$targetPath) . rand_str( 32 ) .'.jpg';

            move_uploaded_file($tempFile,$targetFile);
            echo str_replace($_base_path .'/','',$targetFile);
            
            $_output = false;                          
        }
        
        break;
        
    case 'delete_image':
        
        $image_to_del = $_base_path .'/'. $_POST['image'];
        unlink( $image_to_del );
        
        break;    
    
    case 'delete_image_old':
        
        $image_id = $_POST['id'];
        $deleted = $Files_location->delete( array( 'id' => $image_id ) );
        $_ajax_message = 'OK';
        $_ajax_data['deleted'] = $deleted;
        
        break;    
    
    case 'country':
        
        $_page_content .= $current_page->fetch( 'location_country.tpl' );
        
        break;
    
    case 'country-list':
        
        $country_list = $Country->get_all();
        $current_page->assign( 'countries', $country_list );
        $_page_content .= $current_page->fetch( 'location_country_list_ajax.tpl' );
        
        $_ajax_data['countries'] = $_page_content;
        
        break;
    
    case 'country-add':
        
        $country = $_POST['country'];
        $Country->add( array( 'name' => $country ) );
        
        break;
    
    case 'country-delete':
        
        $id = $_POST['id'];
        $result = $Country->delete( array( 'id' => $id ) );
        
        $_ajax_data['deleted'] = $result;
        
        break;
    
    case 'country-modify':
        
        $id = $_POST['id'];
        $name = $_POST['name'];
        $result = $Country->modify_data_for_id( $id, array( 'name' => $name ) );
    
        $_ajax_data['modified'] = $result;
        
        break;
    
    case 'county':
        
        $countries = $Country->get_all(  );
        $current_page->assign( 'countries', $countries );
        
        $_page_content .= $current_page->fetch( 'location_county.tpl' );
        
        break;
    
    case 'county-list':
        
        $counties = $County->get_all( $Country->TABLE_NAME, array( $County->TABLE_COLUMNS['country'], $Country->TABLE_COLUMNS['id'] ) );
        
        $current_page->assign( 'counties', $counties );
        $counties_html = $current_page->fetch( 'location_county_list_ajax.tpl' );
        
        $_ajax_data['counties'] = $counties_html;
        
        break;
    
    case 'county-add':
        
        $county_name = $_POST['county'];
        $country_id = $_POST['country'];
        $county_population = $_POST['population'];
        $county_male = $_POST['male'];
        $county_urban = $_POST['urban'];
        
        $result = $County->add( array( 
            'county' => $county_name, 
            'country' => $country_id,
            'population' => $county_population,
            'male'  => $county_male,
            'urban' => $county_urban
            ) );
        
        if ( $result )
        {
            $_ajax_message = 'OK';
        }
        
        break;
    
    case 'county-delete':
        
        $county_id = $_POST['id'];
        
        $result = $County->delete( array( 'id' => $county_id ) );
        if ( $result )
        {
            $_ajax_message = 'OK';
            $_ajax_data['deleted'] = $result;
        }
        else
        {
            $_ajax_message = 'ERROR';
            $_ajax_data['deleted'] = $result;
        }
        
        break;
        
    case 'county-modify':
        
        $county_id = $_POST['id'];
        $county_name = $_POST['name'];
        $county_population = $_POST['population'];
        $county_male = $_POST['male'];
        $county_urban = $_POST['urban'];
        
        $result = $County->modify_data_for_id( 
                $county_id, 
                array( 
                    'county' => $county_name,
                    'population' => $county_population,
                    'male'  => $county_male,
                    'urban' => $county_urban
                    ) );
        if ( $result )
        {
            $_ajax_message = 'OK';
            $_ajax_data['modified'] = $result;
        }
        else
        {
            $_ajax_message = 'ERROR';
            $_ajax_data['modified'] = $result;
        }
        
        break;
        
    case 'city':
        
        $countries = $Country->get_all();
        $current_page->assign( 'countries', $countries );
        
        $_page_content .= $current_page->fetch( 'location_city.tpl' );
        
        
        break;
    
    case 'county-list-ajax':
        
        $country_id = $_POST['country_id'];
        $counties = $County->find_by( 'country', $country_id );
        $_ajax_data['counties'] = $counties;
        $_ajax_message = 'OK';
        
        break;
    
    case 'city-list':
        
        $cities = $City->get_all( 
                $County->TABLE_NAME, 
                array( 
                    $City->TABLE_COLUMNS['county'], 
                    $County->TABLE_COLUMNS['id'] 
                        ),
                array(
                    'county' => (int)$_POST['county']
                ) );
        
        $_ajax_message = 'OK';
        $_ajax_data['cities'] = $cities;
        
        $current_page->assign( 'cities', $cities );
        $_page_content = $current_page->fetch( 'location_city_list.tpl' );
        $_ajax_data['cities_html'] = $_page_content;
        $_ajax_data['cities'] = $cities;
        
        break;
        
    case 'city-modify':
        
        $city_name = $_POST['city_name'];
        $city_id = $_POST['city_id'];
        $city_population = $_POST['city_population'];
        $city_male = $_POST['city_male'];
        $city_urban = $_POST['city_urban'];
        
        $City->modify_data_for_id(
                $city_id, array( 
                    'name' => $city_name,
                    'population' => $city_population,
                    'male' => $city_male,
                    'urban' => $city_urban
                    ) 
                );
        
        break;
    
    case 'city-delete':
        
        $city_id = $_POST['city_id'];
        
        $City->delete( array( 'id' => $city_id ) );
        
        break;
    
    case 'city-add':
        
        $city = $_POST['city'];
        $county = $_POST['county'];
        $population = $_POST['population'];
        $male = $_POST['male'];
        $urban = $_POST['urban'];
        
        $result = $City->add( array( 
            'county' => $county,
            'city'  => $city,
            'population' => $population,
            'male'  => $male,
            'urban' => $urban
            ) );
        
        break;
    
    case 'add':

        $countries = $Country->get_all();
        $current_page->assign( 'countries', $countries );
        $current_page->assign( 'action', 'add' );
        
        $location_types = $Location_type->get_all();
        $current_page->assign( 'location_types', $location_types );
        
        $location_types_array = array();
        
        foreach( $location_types as $location_types_item )
        {
            $location_types_array[$location_types_item[$Location_type->TABLE_COLUMNS['id']]] = $location_types_item[$Location_type->TABLE_COLUMNS['type']];
        }
        
        $current_page->assign( 'location_type', $location_types_array );
        
        $_page_content .= $current_page->fetch( 'location_add_modify.tpl' );
        
        break;
            
    case 'modify-action':
        $modify = true;
    case 'add-action':
        $modify = isset( $modify ) ? $modify : false;
        
        if ( !$modify )
        {
            $result = $Location->add(
                    array(
                        'location' => filter_cross( $_POST['location'] ),
                        'location_type'  => $_POST['type'],
                        'outdoor_indoor' => $_POST['outdoor_indoor'],
                        'city' => $_POST['city'],
                        'address' => $_POST['address'],
                        'lng' => $_POST['coord_lng'],
                        'lat' => $_POST['coord_lat'],
                        'zoom' => $_POST['coord_zoom']
                            )
                    );
        }
        else
        {
            $result = $location_id = $_POST['location_id'];
            
            $Location->modify_data_for_id( $location_id,
                        array(
                            'location' => filter_cross( $_POST['location'] ),
                            'location_type'  => $_POST['type'],
                            'outdoor_indoor' => $_POST['outdoor_indoor'],
                            'city' => $_POST['city'],
                            'address' => $_POST['address'],
                            'lng' => $_POST['coord_lng'],
                            'lat' => $_POST['coord_lat'],
                            'zoom' => $_POST['coord_zoom']
                        )
                    );
        }
        
        if ( isset( $_POST['pictures'] ) )
        {
            foreach( $_POST['pictures'] as $pic )
            {
                $pic_name = str_replace( 'temp_pictures/location/', '', $pic );
                $pic_name = str_replace( '.jpg', '', $pic_name );

                $new_name = $_base_path .'/pictures_added/location/'. $pic_name .'.jpg';            
                rename( $pic, $new_name );

                $Files_location->add( 
                        array( 
                            'name'      =>  $new_name,
                            'location'    =>  $result
                            ) 
                        );
            }
        }
        
        if ( $result )
        {
            $_ajax_message = 'OK';
        }
        else
        {
            $_ajax_message = $result;
        }
        
        break;
        
    case 'list':
        
        $locations = $Location->get_all( 
                $City->TABLE_NAME,   
                array(
                    $Location->TABLE_COLUMNS['city'],
                    $City->TABLE_COLUMNS['id']
                ) );
        
        $_ajax_message = 'OK';
        $_ajax_data['locations'] = $locations;
        
        $location_types = $Location_type->get_all();
        
        $location_types_array = array();
        
        foreach( $location_types as $location_types_item )
        {
            $location_types_array[$location_types_item[$Location_type->TABLE_COLUMNS['id']]] = $location_types_item[$Location_type->TABLE_COLUMNS['type']];
        }
        
        $current_page->assign( 'locations', $locations );
        $current_page->assign( 'location_type', $location_types_array );
        $_page_content = $current_page->fetch( 'location_list_ajax.tpl' );
        $_ajax_data['locations_html'] = $_page_content;
        
        break;
            
    case 'delete':
        
        $result_files = $Files_location->delete( array( 'location' => $_POST['location_id'] ) );
        $result = $Location->delete( array( 'id' => $_POST['location_id'] ) );
        
        break;
    
    case 'modify':
        
        $location_id = $URL[2];
        $current_page->assign( 'action', 'modify' );
        
        $countries = $Country->get_all();
        $current_page->assign( 'countries', $countries );        
        
        $location_types = $Location_type->get_all();
        $current_page->assign( 'location_types', $location_types );
        $location_types_array = array();
        foreach( $location_types as $location_types_item )
        {
            $location_types_array[$location_types_item[$Location_type->TABLE_COLUMNS['id']]] = $location_types_item[$Location_type->TABLE_COLUMNS['type']];
        }
        $current_page->assign( 'location_type', $location_types_array );
        
        $pictures = $Files_location->find_by( 'location', $location_id );
        foreach( $pictures as &$p )
        {
            $name_parts = explode( '/', $p[$Files_location->TABLE_COLUMNS['name']] );
            $name_parts_count = count( $name_parts );
            $name_aux = $name_parts[ $name_parts_count - 1 ];
            $name_aux_parts = explode( '.', $name_aux );
            $name_aux = $name_aux_parts[ 0 ];
            $p['id'] = $name_aux;
        }
        $current_page->assign( 'pictures', $pictures );
        
        $location = $Location->find_by( 'id', $location_id );
        if ( count( $location ) == 1 ) 
        {
            $location = $location[0];
            $current_page->assign( 'location', $location );
            
            $city = $City->find_by( 'id', $location[$Location->TABLE_COLUMNS['city']] );
            if ( count( $city ) == 1 )
            {
                $city = $city[0];
                
                $current_page->assign( 'city', $city );
                
                $county = $County->find_by( 'id', $city[$City->TABLE_COLUMNS['county']] );
                if ( count( $county ) == 1 )
                {
                    $county = $county[0];
                    
                    $current_page->assign( 'county', $county );
                    
                    $country = $Country->find_by( 'id', $county[$County->TABLE_COLUMNS['country']] );
                    if ( count( $country ) == 1 )
                    {
                        $country = $country[0];
                        
                        $current_page->assign( 'country', $country );
                    }
                }
            }

            my_DBG( $location, 1 );
            my_DBG( $city, 1 );
            my_DBG( $county, 1 );
            my_DBG( $country, 1 );

            $_page_content = $current_page->fetch( 'location_add_modify.tpl' );            
        }
        break;
    
    case 'modify-action':
        
        $name = $_POST['name'];
        $id = $_POST['id'];
        $outdoor = $_POST['outdoor'];
        $address = $_POST['address'];
        $type = $_POST['type'];
        
        $result = $Location->modify_data_for_id(
                    $id,
                    array(
                        'name' => $name,
                        'outdoor' => $outdoor,
                        'address' => $address,
                        'type' => $type
                    )
                );
        if ( $result )
        {
            $_ajax_message = 'OK';
        }
        else
        {
            $_ajax_message = 'ERROR';
        }
        
    case '':
    default:
        
        $_page_content .= $current_page->fetch( 'location.tpl' );
        
        break;
}
?>